import { defineStore } from 'pinia'
import { login, logout } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: getToken()
  }),
  actions: {
    async login(userInfo) {
      const { username, password } = userInfo
      const res = await login({ username, password })
      setToken(res.data)
      this.token = res.data
    },
    async logout() {
      await logout()
      removeToken()
      this.token = null
    }
  }
}) 